#include "key.h"
#include "delay.h"

/*!
* @brief button initialization 
* @param None
* @return None
*/
void key_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    /* Reset GPIO init structure parameters values */
    GPIO_InitStruct.GPIO_Pin    = KEY_UP_Pin;
    GPIO_InitStruct.GPIO_Mode   = GPIO_Mode_IN;
    GPIO_InitStruct.GPIO_Speed  = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_OType  = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd   = GPIO_PuPd_DOWN;//Keep the normally low level 
    GPIO_Init(KEY_UP_GPIO, &GPIO_InitStruct);
    
    GPIO_InitStruct.GPIO_Pin    = KEY0_Pin | KEY1_Pin | KEY2_Pin;
    GPIO_InitStruct.GPIO_PuPd   = GPIO_PuPd_UP;//Keep the normally high level 
    GPIO_Init(KEY0_GPIO, &GPIO_InitStruct);
}


/*!
* @brief key scan 
* @param None
* @return uint8_t 
*/
uint8_t key_scan(void)
{
    uint8_t key = 0;
    
    if(KEY_UP_VAL == 1 | KEY0_VAL == 0 | KEY1_VAL == 0 | KEY2_VAL == 0)
    {
        Delay(50);
        if(KEY_UP_VAL == 1)
            key = 0x33;
        if(KEY0_VAL == 0)
            key = 0x30;
        if(KEY1_VAL == 0)
            key = 0x31;
        if(KEY2_VAL == 0)
            key = 0x32;
    }
    return key;
}
